﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OracleClient;

namespace Model.Sec
{
    public class Node
    {
        public Node()
        {

        }

        #region Node的属性
        private string _NodeISN;
        /// <summary>
        /// 节点ISN
        /// </summary>
        public string NodeISN
        {
            get { return _NodeISN; }
            set { this._NodeISN = value; }
        }
        private string _NodeID;
        /// <summary>
        /// 节点ID
        /// </summary>
        public string NodeID
        {
            get { return _NodeID; }
            set { _NodeID = value; }
        }
        private string _NodeName;
        /// <summary>
        /// 节点名称
        /// </summary>
        public string NodeName
        {
            get { return _NodeName; }
            set { _NodeName = value; }
        }
        private DateTime _CreateTime;
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime
        {
            get { return _CreateTime; }
            set { _CreateTime = value; }
        }
        private string _NodeLevel;
        /// <summary>
        /// 节点级别
        /// </summary>
        public string NodeLevel
        {
            get { return _NodeLevel; }
            set { _NodeLevel = value; }
        }
        private string _UpNodeID;
        /// <summary>
        /// 上级节点ID
        /// </summary>
        public string UpNodeID
        {
            get { return _UpNodeID; }
            set { _UpNodeID = value; }
        }
        private string _UpNodeName;
        /// <summary>
        /// 上级节点名称
        /// </summary>
        public string UpNodeName
        {
            get { return _UpNodeName; }
            set { _UpNodeName = value; }
        }
        private string _NodeType;
        /// <summary>
        /// 节点类型
        /// </summary>
        public string NodeType
        {
            get { return _NodeType; }
            set { _NodeType = value; }
        }
        private int _NodeIndex;
        /// <summary>
        /// 显示顺序
        /// </summary>
        public int NodeIndex
        {
            get { return _NodeIndex; }
            set { _NodeIndex = value; }
        }
        private int _IsCatalog;
        /// <summary>
        /// 是否为目录
        /// </summary>
        public int IsCatalog
        {
            get { return _IsCatalog; }
            set { _IsCatalog = value; }
        }
        private float _LNG;
        /// <summary>
        /// 经度
        /// </summary>
        public float LNG
        {
            get { return _LNG; }
            set { _LNG = value; }
        }
        private float _LAT;
        /// <summary>
        /// 纬度
        /// </summary>
        public float LAT
        {
            get { return _LAT; }
            set { _LAT = value; }
        }
        private string _DisplayLevel;
        /// <summary>
        /// 显示级别
        /// </summary>
        public string DisplayLevel
        {
            get { return _DisplayLevel; }
            set { _DisplayLevel = value; }
        }
        private string _Strip;

        public string Strip
        {
            get { return _Strip; }
            set { _Strip = value; }
        }
        private string _ArmyType;
        public string ARMYTYPE
        {
            set { _ArmyType = value; }
            get { return _ArmyType; }
        }
        private string _ArmyLevel;
        public string ArmyLevel
        {
            set { _ArmyLevel = value; }
            get { return _ArmyLevel; }
        }
        private string _MaintainArmyname;
        public string MAINTAINARMYNAME
        {
            set { _MaintainArmyname = value; }
            get { return _MaintainArmyname; }
        }
        private string _Armyid;
        public string ARMYID
        {
            set { _Armyid = value; }
            get { return _Armyid; }
        }
        private UnitCollection _UnitCollection;
        /// <summary>
        /// 节点下的所有单位的集合
        /// </summary>
        public UnitCollection Units
        {
            get
            {
                if (this._UnitCollection == null)
                    this.GetUnitsByNode();
                return this._UnitCollection;
            }
        }
        private NodeCollection _ChildNodes;
        /// <summary>
        /// 获得子节点的集合
        /// </summary>
        public NodeCollection ChildNodes
        {
            get
            {
                if (this._ChildNodes == null)
                    this._GetChildNodes();
                return this._ChildNodes;
            }
        }
        #endregion

        #region Node的私有函数
        /// <summary>
        /// 查找节点下的所有单位
        /// </summary>
        /// <param name="NodeISN">节点ISN</param>
        /// <returns>节点下单位的集合</returns>
        private void GetUnitsByNode()
        {
            string sql = "SELECT * FROM SEC_UNIT WHERE UPNODEID='" + this.NodeISN + "'";
            System.Data.DataTable dt = DBUtility.OracleHelper.Query(sql).Tables[0];
            UnitCollection uc = new UnitCollection(dt.Rows.Count);
            for (int rowNum = 0; rowNum < dt.Rows.Count; rowNum++)
            {
                Unit unit = new Unit();

                unit.UnitISN = dt.Rows[rowNum]["UNITISN"].ToString();
                unit.UpNodeName = dt.Rows[rowNum]["UPNODENAME"].ToString();
                unit.Address = dt.Rows[rowNum]["ADDRESS"].ToString();
                if(dt.Rows[rowNum]["CREATETIME"].ToString()!="")
                unit.Createtime = Convert.ToDateTime(dt.Rows[rowNum]["CREATETIME"]);
                unit.Description = dt.Rows[rowNum]["DESCRIPTION"].ToString();
                unit.Tel = dt.Rows[rowNum]["TEL"].ToString();
                unit.UnitName = dt.Rows[rowNum]["UNITTIME"].ToString();
                unit.UnitSort = dt.Rows[rowNum]["UNITSORT"].ToString();
                //this.UnitType = dr["UNITTYPE"].ToString();
                unit.UpNodeID = dt.Rows[rowNum]["UPNODEISN"].ToString();

                uc[rowNum] = unit;
            }
            this._UnitCollection = uc;
            throw new NotImplementedException("参考《系统管理移植注意事项.doc》2.3.1进行数据转换");
        }
        /// <summary>
        /// 
        /// 查询子结点
        /// <param name="NodeId">节点ID</param>
        /// <returns>节点下所有子结点的集合</returns>
        /// </summary>

        private void _GetChildNodes()
        {

            string sql = "select * from sec_Node where upnodeid='" + this.NodeID + "'";
            System.Data.DataTable dt = DBUtility.OracleHelper.Query(sql).Tables[0];
            NodeCollection nc = new NodeCollection(dt.Rows.Count);
            for (int rowNom = 0; rowNom < dt.Rows.Count; rowNom++)
            {
                Node node = new Node();
                node.ARMYID = dt.Rows[rowNom]["ARMYID"].ToString();
                node.ARMYTYPE = dt.Rows[rowNom]["ARMYTYPE"].ToString();
                if(dt.Rows[rowNom]["CREATETIME"].ToString()!="")
                node.CreateTime = Convert.ToDateTime(dt.Rows[rowNom]["CREATETIME"]);
                node.DisplayLevel = dt.Rows[rowNom]["DISPLAYLEVEL"].ToString();
                node.IsCatalog = int.Parse(dt.Rows[rowNom]["ISCATALOG"].ToString());
                node.LAT = float.Parse(dt.Rows[rowNom]["LAT"].ToString());
                node.LNG = float.Parse(dt.Rows[rowNom]["LNG"].ToString());
                node.MAINTAINARMYNAME = dt.Rows[rowNom]["MAINTAINARMYNAME"].ToString();
                node.NodeID = dt.Rows[rowNom]["NODEID"].ToString();
                node.NodeIndex = Convert.ToInt32(dt.Rows[rowNom]["NODEINDEX"]);
                node.NodeISN = dt.Rows[rowNom]["NODEISN"].ToString();
                node.NodeLevel = dt.Rows[rowNom]["NODELEVEL"].ToString();
                node.NodeName = dt.Rows[rowNom]["NODENAME"].ToString();
                node.NodeType = dt.Rows[rowNom]["NODETYPE"].ToString();
                node.Strip = dt.Rows[rowNom]["STRIP"].ToString();

                nc[rowNom] = node;

            }
            this._ChildNodes = nc;
            // throw new  NotImplementedException();
        }
        #endregion
    }
}
